﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;

namespace DTO
{
    public class FuncOfDatatable
    {
        /// <summary>
        /// Determine of specified type is nullable
        /// </summary>
        public static bool IsNullable(Type t)
        {
            return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
        }

        /// <summary>
        /// Return underlying type if type is Nullable otherwise return the type
        /// </summary>
        public static Type GetCoreType(Type t)
        {
            if (t != null && IsNullable(t))
            {
                if (!t.IsValueType)
                {
                    return t;
                }
                else
                {
                    return Nullable.GetUnderlyingType(t);
                }
            }
            else
            {
                return t;
            }
        }
        /// <summary>
        /// <typeparam name=”T”>Custome Class </typeparam>
        /// <param name=”lst”>List Of The Custome Class</param>
        /// <returns> Return the class datatbl </returns>        
        /// </summary>
        public static DataTable ToDataTable<T>(List<T> ls)
        {
            var tb = new DataTable(typeof(T).Name);

            PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo prop in props)
            {
                Type t = GetCoreType(prop.PropertyType);
                tb.Columns.Add(prop.Name, t);
            }

            foreach (T item in ls)
            {
                var values = new object[props.Length];

                for (int i = 0; i < props.Length; i++)
                {
                    values[i] = props[i].GetValue(item, null);
                }

                tb.Rows.Add(values);
            }
            return tb;
        }

        public static void AddColumn(ref DataTable dt, string Name, string Type)
        {
            DataColumn column = new DataColumn();
            column.DataType = System.Type.GetType(Type);
            column.ColumnName = Name;            
            dt.Columns.Add(column);
        }
        
        public static void SwapRow(ref DataTable dt, int iX, int iY)
        {
            DataRow rowTemp = dt.NewRow();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                rowTemp[i] = dt.Rows[iX][i];
            }

            for(int i =0;i< dt.Columns.Count;i++)
            {
                dt.Rows[iX][i] = dt.Rows[iY][i];
            }
            
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                dt.Rows[iY][i] = rowTemp[i];
            }
        }

        public static void MixRowInDatatable(ref DataTable dt)
        {
            Random iRn = new Random();
            
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                SwapRow(ref dt, i, iRn.Next(dt.Rows.Count - 1));                
            }
        }
    }
}
